package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.*;
import com.zb.extend.WorkFinishOrderTemplateExtend;
import com.zb.mapper.ZjWorkFinishOrderTemplateMapper;
import com.zb.mobile.NetworkMobileStatementTemplateVo;
import com.zb.result.PageUtil;
import com.zb.service.ZjWorkFinishOrderTemplateFieldValOptionService;
import com.zb.service.ZjWorkFinishOrderTemplateService;
import com.zb.service.ZjWorkStatementTemplateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 工单结单模板保存表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-11-30
 */
@Service
public class ZjWorkFinishOrderTemplateServiceImpl extends ServiceImpl<ZjWorkFinishOrderTemplateMapper, ZjWorkFinishOrderTemplate> implements ZjWorkFinishOrderTemplateService {
    @Resource
    private ZjWorkFinishOrderTemplateFieldValServiceImpl workFinishOrderTemplateFieldValService;
    @Resource
    private ZjWorkFinishOrderTemplateFieldValOptionService workFinishOrderTemplateFieldValOptionService;
    @Resource
    private ZjWorkStatementTemplateService workStatementTemplateService;

    // 保存工单结单模板
    @Override
    public void save(Long custAccId, Long workId, Long finishOrderId, List<NetworkMobileStatementTemplateVo> templateVos) {
        if (CollectionUtil.isEmpty(templateVos)) {
            return;
        }
        List<ZjWorkFinishOrderTemplate> finishOrderTemplates = new ArrayList<>();
        ZjWorkFinishOrderTemplate orderTemplate = null;
        List<ZjWorkFinishOrderTemplateFieldVal> templateFieldVals = new ArrayList<>();
        ZjWorkFinishOrderTemplateFieldVal fieldVal = null;
        List<ZjWorkFinishOrderTemplateFieldValOption> valOptions = new ArrayList<>();
        ZjWorkFinishOrderTemplateFieldValOption option = null;

        for (NetworkMobileStatementTemplateVo vo : templateVos) {
            orderTemplate = new ZjWorkFinishOrderTemplate();
            orderTemplate.setId(IdWorker.getId());
            orderTemplate.setCustAccId(custAccId);
            orderTemplate.setWorkId(workId);
            orderTemplate.setWorkFinishOrderId(finishOrderId);
            orderTemplate.setWorkStatementTemplateId(vo.getId());
            finishOrderTemplates.add(orderTemplate);
            for (FieldValVo valVo : vo.getFieldValVos()) {
                fieldVal = new ZjWorkFinishOrderTemplateFieldVal();
                BeanUtil.copyProperties(valVo, fieldVal);
                fieldVal.setId(IdWorker.getId());
                fieldVal.setCustAccId(custAccId);
                fieldVal.setWorkId(workId);
                fieldVal.setWorkFinishOrderTemplateId(orderTemplate.getId());
                fieldVal.setWorkStatementTemplateFieldId(valVo.getFieldId());
                templateFieldVals.add(fieldVal);
                for (FieldValOptionVo optVo : valVo.getOptVos()) {
                    option = new ZjWorkFinishOrderTemplateFieldValOption();
                    option.setId(IdWorker.getId());
                    option.setCustAccId(custAccId);
                    option.setWorkId(workId);
                    option.setWorkFinishOrderTemplateId(orderTemplate.getId());
                    option.setWorkFinishOrderTemplateFieldValId(fieldVal.getId());
                    option.setOptValue(null == optVo.getOptValue()? "" : optVo.getOptValue());
                    valOptions.add(option);
                }
            }
        }
        this.saveBatch(finishOrderTemplates);
        if (CollectionUtil.isNotEmpty(templateFieldVals)) workFinishOrderTemplateFieldValService.saveBatch(templateFieldVals);
        if (CollectionUtil.isNotEmpty(valOptions)) workFinishOrderTemplateFieldValOptionService.saveBatch(valOptions);
    }

    // 根据结单ID获取结单模板
    @Override
    public List<WorkFinishOrderTemplateExtend> getByWorkFinishOrderId(Long finishOrderId) {
        QueryWrapper<ZjWorkFinishOrderTemplate> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_id", "work_statement_template_id")
                .eq("work_finish_order_id", finishOrderId);
        List<ZjWorkFinishOrderTemplate> templateList = this.list(wrapper);
        if (CollectionUtil.isEmpty(templateList)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkStatementTemplate> statementTemplateQueryWrapper = new QueryWrapper<>();
        statementTemplateQueryWrapper.select("id", "name")
                .in("id", templateList.stream().map(ZjWorkFinishOrderTemplate::getWorkStatementTemplateId).distinct().toList());
        List<ZjWorkStatementTemplate> statementTemplateList = workStatementTemplateService.list(statementTemplateQueryWrapper);
        return templateList.stream().map(template -> {
            WorkFinishOrderTemplateExtend extend = new WorkFinishOrderTemplateExtend();
            BeanUtil.copyProperties(template, extend);
            statementTemplateList.stream().filter(f -> Objects.equals(f.getId(), template.getWorkStatementTemplateId()))
                    .findFirst().map(ZjWorkStatementTemplate::getName).ifPresent(extend::setTemplate);
            return extend;
        }).toList();
    }
}
